<template>
  <div>
    <!-- 搜索区 -->

    <div style="height: 80px; margin-top: 20px">
      <el-form :inline="true" :model="formInline" class="demo-form-inline">
        <el-form-item label="账号信息">
          <el-input v-model="formInline.account" placeholder="账号信息"></el-input>
        </el-form-item>
        <el-form-item label="状态信息">
          <el-select v-model="formInline.status" placeholder="状态信息">
            <el-option label="区域一" value="shanghai"></el-option>
            <el-option label="区域二" value="beijing"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onSubmit">查询</el-button>
          <el-button type="primary" plain @click="add(null)">新增</el-button>
        </el-form-item>
      </el-form>
    </div>
    <!-- 新增和编辑对话框 -->
    <el-dialog :title="dialog.title" :visible.sync="dialogFormVisible">
      <el-form :model="form">
        <el-form-item label="用户名:" :label-width="formLabelWidth">
          <el-input v-model="form.account" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="密码:" :label-width="formLabelWidth">
          <el-input v-model="form.password" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="状态:" :label-width="formLabelWidth">
          <el-input v-model="form.status" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitForm">确 定</el-button>
      </div>
    </el-dialog>
    <!-- 表格区 -->
    <el-table :data="tableData" style="width: 100%" empty-text="你来到了没有数据的荒野">
      <el-table-column prop="id" label="id" width="100"> </el-table-column>
      <el-table-column prop="account" label="账户名" width="100">
      </el-table-column>
      <el-table-column prop="password" label="密码"> </el-table-column>
      <el-table-column prop="role_id" label="权限id" width="80">
      </el-table-column>
      <el-table-column prop="roles" label="权限" width="80"> </el-table-column>
      <el-table-column prop="status" label="状态" width="80"> </el-table-column>
      <el-table-column prop="edit" label="编辑" width="120">
        <el-breadcrumb separator="|" slot-scope="scope">
          <el-breadcrumb-item
            ><el-button type="text" @click="open(scope.row)"
              >删除</el-button
            ></el-breadcrumb-item
          >
          <el-breadcrumb-item
            ><el-button type="text" @click="add(scope.row)"
              >编辑</el-button
            ></el-breadcrumb-item
          >
        </el-breadcrumb>
      </el-table-column>
    </el-table>
    <page
      :num="pageinit.total"
      @changeSize="changeSize"
      @changeCurrent="changeCurrent"
    />
  </div>
</template>
<style>
.el-form-item__label {
  float: left;
}
</style>
<script>
import Page from "@/components/Page.vue";
import axios from "axios";
export default {
  data() {
    return {
      // 表格数据
      tableData: [],
      //弹出窗标题
      dialog: {
        title: ""
      },
      //分页信息
      pageinit: {
        currentPage: 1,
        pageSize: 10,
        total: 0,
      },
      //搜索区表格数据
      formInline: {
        account: "",
        status: "",
      },
      //控制弹出窗标志位
      dialogFormVisible: false,
      //弹出窗数据
      form: {
        id: "",
        account: "",
        password: "",
        status: "",
      },
      formLabelWidth: "80px",
      //区分修改还是新增的标志位
      changeFlag: false,
    };
  },

  components: {
    Page,
  },

  created() {
    this.findAllManager();
  },

  methods: {
    //分页查找所有用户数据
    findAllManager() {
      // alert(1)
      axios.get(
          "/rbacManager/User/" +
            this.pageinit.currentPage +
            "/" +
            this.pageinit.pageSize
        )
        .then((res) => {
          console.log(res);
          if (res.data.status == 200) {
            this.tableData = res.data.data.list;
            this.pageinit.total = res.data.data.total;
          }
        })
        .catch(() => {});
    },
    //改变当前页
    changeCurrent(val) {
      this.pageinit.currentPage = val;
      this.findAllManager();
    },
    //改变分页大小
    changeSize(val) {
      this.pageinit.pageSize = val;
      this.pageinit.currentPage = 1;
      this.findAllManager();
    },
    //根据传入数据进行新增或编辑操作
    add(row) {
      if (row == null) {
        this.dialog.title = "新增用户"
        this.changeFlag = false;
        this.form.account = "";
        this.form.password = "";
        this.form.status = "";
      } else {
        this.dialog.title = "编辑用户"
        this.changeFlag = true;
        this.form.id = row.id;
        this.form.account = row.account;
        this.form.password = row.password;
        this.form.status = row.status;
      }
      this.dialogFormVisible = true;
    },
    //提交弹出窗数据，进行新增或者编辑操作
    submitForm() {
      if (this.changeFlag) {
        this.$axios
          .put("/rbacManager/User", this.form)
          .then((res) => {
            console.log(res);
            if (res.data.status == 200) {
              this.$message({
                showClose: true,
                message: "恭喜你成功修改数据",
                type: "success",
              });
              this.dialogFormVisible = false;
              this.findAllManager();
            }
          })
          .catch();
      } else {
        this.$axios
          .post("/rbacManager/User", this.form)
          .then((res) => {
            console.log(res);
            if (res.data.status == 200) {
              this.$message({
                showClose: true,
                message: "恭喜你成功添加数据",
                type: "success",
              });
              this.dialogFormVisible = false;
              this.findAllManager();
            }
          })
          .catch();
      }
    },
    //进行模糊查询
    onSubmit() {
      this.$axios({
          method: "post",
          url: "/rbacManager/User/"+this.pageinit.currentPage+"/" +this.pageinit.pageSize,
          data: this.formInline,  
        })
        .then((res) => {
          console.log(res);
          if (res.data.status == 200) {
            this.tableData = res.data.data.list;
            this.pageinit.total = res.data.data.total;
          }
        })
        .catch();
    },
    //弹出删除确认窗
    open(row) {
      this.$confirm("此操作将永久删除此条数据, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.delete(row);
          this.$message({
            type: "success",
            message: "删除成功!",
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    //删除数据
    delete(row) {
      this.$axios
        .delete("/rbacManager/User/" + row.id)
        .then((res) => {
          console.log(res);
          if (res.data.status == 200) {
            this.findAllManager();
          }
        })
        .catch();
    },
  },
  watch: {
    $route(to, from) {
      console.log(to);
      console.log(from);
    },
  },
};
</script>